package jsyf.oa.service.report;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.LeaveReportDao;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.facade.report.LeaveReportFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class LeaveReportService implements LeaveReportFacade {

    @Autowired
    private LeaveReportDao leaveReportDao;
    @Autowired
    private CompanyDao companyDao;



    /**
     * 区域支持-获取请假列表
     * @param jsonData
     * @return
     */
    @Override
    public List<JSONObject> getLeaveGroup(String jsonData) {

        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String isToday = jsonParams.getString("isToday");
        String month = jsonParams.getString("month");
        //所有公司
        List<JSONObject> companyList = companyDao.getJSONListByCondition(" AND DEL_FLAG=0  ORDER BY SORT_NO ", null);
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < companyList.size(); i++) {
            String companyId = companyList.get(i).get("id").toString();
            String companyName = companyList.get(i).get("name").toString();
            JSONObject result = leaveReportDao.getLeaveByCompanyId(companyId,isToday,month);
            result.put("companyName",companyName);
            list.add(result);
        }
        return list;
    }

    /**
     * 根据公司id或区域id获取请假人次
     * @param jsonData
     * @return
     */
    @Override
    public List<JSONObject> getLeavePersonList(String jsonData) {

        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String isToday = jsonParams.getString("isToday");
        String month = jsonParams.getString("month");
        String companyId = jsonParams.getString("companyId");
        String superiorId = jsonParams.getString("superiorId");
        String supportId = jsonParams.getString("supportId");
        String departId = jsonParams.getString("departId");
        String userId = jsonParams.getString("userId");
        List<JSONObject> list = new ArrayList<>();
        if (companyId!=null && companyId.length()>0){
            list = leaveReportDao.getleaveListByCompanyId(companyId,isToday,month);
        }else if (departId!=null && departId.length()>0) {
            list = leaveReportDao.getleaveListByDepartId(departId,isToday,month);
        }else if (superiorId!=null && superiorId.length()>0){
            list = leaveReportDao.getSubordinateLeaveListByUserId(superiorId,isToday,month);
        }else if (supportId!=null && supportId.length()>0){
            list = leaveReportDao.getLeaveListBySupportId(supportId,isToday,month);
        }else if (userId!=null && userId.length()>0) {
            list = leaveReportDao.getLeaveListByUserId(userId,month);
        }
        return list;
    }


    /**
     * 获取集团所有请假信息
     * @param jsonData
     * @return
     */
    @Override
    public JSONObject getLeaveInfo(String jsonData) {

        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);

        String groupId = jsonParams.getString("groupId");
        String companyId = jsonParams.getString("companyId");
        String superiorId = jsonParams.getString("superiorId");
        String supportId = jsonParams.getString("supportId");
        String departId = jsonParams.getString("departId");
        String userId = jsonParams.getString("userId");
        JSONObject jsonObject = leaveReportDao.getLeaveCount(userId,departId,supportId,superiorId,companyId,groupId).get(0);
        return jsonObject;
    }

    @Override
    public List<JSONObject> getLeaveIsTodayInfo(String jsonData) {
        return leaveReportDao.getleaveListByDateTime();
    }

}
